package com.fg.pro.sys.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fg.pro.model.config.ValidationGroups;
import com.fg.pro.model.reponse.ResponseResult;
import com.fg.pro.model.sys.entity.SysMenu;
import com.fg.pro.model.sys.entity.SysRole;
import com.fg.pro.model.sys.entity.SysRoleMenu;
import com.fg.pro.sys.service.SysRoleService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * 角色(SysRole)表控制层
 *
 * @author makejava
 * @since 2025-07-17 10:10:47
 */
@Tag(name = "SysRole",description = "角色")
@RestController
@RequestMapping("sysRole")
public class SysRoleController {
    /**
     * 服务对象
     */
    @Resource
    private SysRoleService sysRoleService;


    @Operation(summary = "获取角色分页",description = "")
    @GetMapping("/getPage")
    public ResponseResult getPage(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                  @RequestParam(required = false) String roleName){
        PageInfo pageInfo = sysRoleService.getPage(pageNum, pageSize, roleName);
        return ResponseResult.okResult(pageInfo);
    }

    @Operation(summary = "获取角色",description = "id")
    @GetMapping("/getById")
    public ResponseResult getById(@RequestParam String id){
        SysRole sysRole = sysRoleService.getById(id);
        return ResponseResult.okResult(sysRole);
    }

    @Operation(summary = "添加角色",description = "sysDept")
    @PutMapping("/save")
    public ResponseResult save(@RequestBody (required = true)@Validated(ValidationGroups.Insert.class)SysRole sysRole){
        boolean save = sysRoleService.save(sysRole);
        return ResponseResult.okResult(save?"添加成功":"添加失败");
    }

    @Operation(summary = "修改角色",description = "sysDept")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody (required = true)@Validated(ValidationGroups.Update.class)SysRole sysRole){
        boolean update = sysRoleService.updateById(sysRole);
        return ResponseResult.okResult(update?"修改成功":"修改失败");
    }

    @Operation(summary = "删除角色",description = "ids")
    @DeleteMapping("/del")
    public ResponseResult del(@RequestParam (required = true)Long[] ids){
        List<Long> list = Arrays.asList(ids);
        boolean del = sysRoleService.removeByIds(list);
        return ResponseResult.okResult(del?"删除成功":"删除失败");
    }

    @Operation(summary = "获取角色全部权限",description = "")
    @GetMapping("/getAllMenuTree")
    public ResponseResult getAllMenuTree(){
        List<SysMenu> list  = sysRoleService.getAllMenuTree();
        return ResponseResult.okResult(list);
    }

    @Operation(summary = "获取角色拥有权限",description = "roleId")
    @GetMapping("/getMenuIdById")
    public ResponseResult getMenuIdById(@RequestParam (required = true)Long roleId){
        List<Long> list  = sysRoleService.getMenuIdById(roleId);
        return ResponseResult.okResult(list);
    }



    @Operation(summary = "修改权限",description = "sysDept")
    @PostMapping("/updateMenu")
    public ResponseResult updateMenu(@RequestParam (required = true)Long roleId,
                                     @RequestParam (required = true)List<Long> menuIdList){
        Boolean update = sysRoleService.updateMenu(roleId,menuIdList);
        return ResponseResult.okResult(update?"修改成功":"修改失败");
    }


}

